package com.douqu.game.mergeManager.mergeUtils;

import com.alibaba.fastjson.JSONObject;
import com.bean.core.util.HttpJsonUtils;
import com.douqu.game.MenuManager;
import com.douqu.game.core.database.model.GMLogModel;
import com.douqu.game.core.database.model.GMModel;
import com.douqu.game.core.database.model.ServerModel;
import com.douqu.game.core.util.CoreUtils;
import com.douqu.game.core.util.LogUtils;

import javax.swing.*;
import java.lang.reflect.Array;
import java.util.*;

/**
 * @Author: Bean
 * @Description:
 * @Date: 2018-03-24 15:31
 */
public class MergeMain {

    public static String MERGE_URL;

    public static void main(String[] args)
    {
//        LoadFactory.loadGameObject("data");
//
//        try {
//            merge(new DatabaseConfig("192.168.2.228", "3306", "slg1", "root", "123456"),
//                    new DatabaseConfig("192.168.2.228", "3306", "slg1002", "root", "123456"),
//                    new DatabaseConfig("192.168.2.228", "3306", "slg1003", "root", "123456"),
//                    new DatabaseConfig("192.168.2.228", "3306", "slg1004", "root", "123456"));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        System.exit(0);

        int a = new DatabaseFactory(new DatabaseConfig("192.168.2.7", "3306", "slg1", "root", "root")).execute("SELECT * FROM server");
        System.out.println(a);
    }



    public static boolean merge(MenuManager manager,DatabaseConfig saveDatabaseConfig, DatabaseConfig... readDatabaseConfig) throws Exception {

        DatabaseFactory saveDatabase = new DatabaseFactory(saveDatabaseConfig);
        int playerCount = saveDatabase.getRealPlayerCount();
        if(playerCount > 0)
        {
            //LogUtils.info("请先清空最终数据库");
            JOptionPane.showMessageDialog(manager.getMainFrame(),"请先清空最终数据库");
            manager.println("请先清空最终数据库");
            return false;
        }
        List<MergePlayer> mergePlayerList = new ArrayList<>();
        List<GMLogModel> gmLogModelList = new ArrayList<>();
        List<GMModel> gmModelList = new ArrayList<>();
        DatabaseFactory readDatabase = null;
        ServerModel serverModel = null;
        for(DatabaseConfig databaseConfig : readDatabaseConfig)
        {
            //LogUtils.info("开始读取数据库 -> " + databaseConfig.getHost() + ":" + databaseConfig.getPort() + "/" + databaseConfig.getDb());
            manager.println("开始读取数据库 -> " + databaseConfig.getHost() + ":" + databaseConfig.getPort() + "/" + databaseConfig.getDb());
            readDatabase = new DatabaseFactory(databaseConfig);
            //处理拍卖行信息
            List<MergePlayer> tempList = readDatabase.getPlayerList();
            //LogUtils.info("玩家数量 -> " + tempList.size());
            manager.println("玩家数量 -> " + tempList.size());
            mergePlayerList.addAll(tempList);

            gmLogModelList.addAll(readDatabase.getGMLogList());
            List<GMModel> list = readDatabase.getGMList();
            for(GMModel gmModel : list)
            {
                if(gmModelList.contains(gmModel))
                    continue;

                gmModelList.add(gmModel);
            }
        }

        Map<String, MergePlayer> userModelMap = new HashMap<>();
        int i = saveDatabase.getRobotCount() + 1;
        MergePlayer tempPlayer = null;
        List<MergePlayer> finalPlayerList = new ArrayList<>();
        for(MergePlayer mergePlayer : mergePlayerList)
        {
            tempPlayer = userModelMap.get(mergePlayer.getUserModel().getAccount());
            if(tempPlayer != null)
            {
                if(CoreUtils.isNullOrEmpty(tempPlayer.getUserModel().getMobile()) && !CoreUtils.isNullOrEmpty(mergePlayer.getUserModel().getMobile()))
                    tempPlayer.getUserModel().setMobile(mergePlayer.getUserModel().getMobile());

                tempPlayer.getPlayerList().addAll(mergePlayer.getPlayerList());
            }
            else
            {
                mergePlayer.setId(++i);
                finalPlayerList.add(mergePlayer);
                userModelMap.put(mergePlayer.getUserModel().getAccount(), mergePlayer);
            }
        }


        LogUtils.info("开始合并数据库 -> 最终玩家数量 -> " + finalPlayerList.size() + ",GM日志记录数量 -> " + gmLogModelList.size());
        manager.println("开始合并数据库 -> 最终玩家数量 -> " + finalPlayerList.size() + ",GM日志记录数量 -> " + gmLogModelList.size());
        saveDatabase.merge(finalPlayerList, gmLogModelList, gmModelList);

        LogUtils.info("数据库合并成功 --------------------------------> ");

        mergeAuth(manager,saveDatabaseConfig,readDatabaseConfig);
        return true;
    }

    public static boolean mergeAuth(MenuManager manager,DatabaseConfig saveDatabaseConfig, DatabaseConfig... readDatabaseConfig){
        /**
         * 发送post请求去认证服
         */
        manager.println("开始发送请求给认证服 修改认证服服务器ID与生成合服记录");
        String merge = "";
        for (int i = 0 ; i < readDatabaseConfig.length; i++){
            merge = merge + readDatabaseConfig[i].getDb().replaceAll("slg","");
            if (i != readDatabaseConfig.length - 1)
                merge = merge + ",";
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("target",saveDatabaseConfig.getDb().replaceAll("slg",""));
        jsonObject.put("merge",merge);
        manager.println("target serverID:" + saveDatabaseConfig.getDb().replaceAll("slg",""));
        manager.println("merge serverIDList:" + merge);
        manager.println("服务器地址 -> " + MERGE_URL);
        manager.println("参数 ->" + jsonObject.toJSONString());
        try {
            JSONObject object = HttpJsonUtils.httpPost(MERGE_URL,jsonObject);
        }catch (Exception e){
            e.printStackTrace();
            manager.println("认证服返回失败 请确认认证服地址！");
            return false;
        }
        manager.println("认证服返回 success");
        return true;
    }
}
